#include "fwCameraSimple.h"

CameraSimple::CameraSimple()
	:Camera3D()
{
	LOGIDBG("Init SimpleCamera\n");
	Projection.SetIdentity().SetPerspective(FOV, ASPECTRATIO, NEAREST, FATHEST);
}

CameraSimple::~CameraSimple()
{
	LOGIDBG("Destroy SimpleCamera\n");
}

Matrix CameraSimple::LocalWorld()
{
	Matrix scale;
	Matrix traslate;
	Matrix rotAll, rotX, rotY, rotZ;
	traslate.SetIdentity().SetTranslation(Position);
	rotX.SetIdentity().SetRotationX(RotateAngle.x);
	rotY.SetIdentity().SetRotationY(RotateAngle.y);
	rotZ.SetIdentity().SetRotationZ(RotateAngle.z);
	rotAll = rotZ*rotX*rotY;
	return rotAll*traslate;
}

Matrix CameraSimple::View()
{
	Matrix scale;
	Matrix translate;
	Matrix rotAll, rotX, rotY, rotZ;
	translate.SetIdentity().SetTranslation(-Position.x, -Position.y, -Position.z);
	rotX.SetIdentity().SetRotationX(-RotateAngle.x);
	rotY.SetIdentity().SetRotationY(-RotateAngle.y);
	rotZ.SetIdentity().SetRotationZ(-RotateAngle.z);
	rotAll = rotY*rotX*rotZ;
	return translate*rotAll;
}

void CameraSimple::MoveForward(FWfloat offset)
{
	Vector4 local = Vector4(0.0f, 0.0f, -offset, 1);
	Vector4 world = local * LocalWorld();
	Position = Vector3(world.x, world.y, world.z);
}

void CameraSimple::MoveBackward(FWfloat offset)
{
	Vector4 local = Vector4(0.0f, 0.0f, offset, 1);
	Vector4 world = local * LocalWorld();
	Position = Vector3(world.x, world.y, world.z);
}

void CameraSimple::MoveLeft(FWfloat offset)
{
	Vector4 local = Vector4(-offset, 0.0f, 0.0f, 1);
	Vector4 world = local * LocalWorld();
	Position = Vector3(world.x, world.y, world.z);
}

void CameraSimple::MoveRight(FWfloat offset)
{
	Vector4 local = Vector4(offset, 0.0f, 0.0f, 1);
	Vector4 world = local * LocalWorld();
	Position = Vector3(world.x, world.y, world.z);
}

void CameraSimple::RotateUp(FWfloat angle)
{
	RotateAngle.x += angle;
	if (RotateAngle.x >= 360.0f)
		RotateAngle.x -= 360.0f;
}

void CameraSimple::RotateDown(FWfloat angle)
{
	RotateAngle.x -= angle;
	if (RotateAngle.x <= -360.0f)
		RotateAngle.x += 360.0f;
}

void CameraSimple::RotateLeft(FWfloat angle)
{
	RotateAngle.y += angle;
	if (RotateAngle.y <= -360.0f)
		RotateAngle.y += 360.0f;
}

void CameraSimple::RotateRight(FWfloat angle)
{
	RotateAngle.y -= angle;
	if (RotateAngle.y >= 360.0f)
		RotateAngle.y -= 360.0f;
}